package com.encyclopedia.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Security全局变量
 *
 * @author by 春风能解释
 * <p>
 * 2024/4/20
 */
@Configuration
@Slf4j
//感谢 https://blog.csdn.net/weixin_45688141/article/details/129309522
public class SecurityGlobalContext {

    /**
     * 密码加密器(用于注册加密)
     *
     * @return 密码加密器
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        log.info("SpringSecurity-密码加密已成功配置!");
        return new BCryptPasswordEncoder();
    }

    /**
     * 基于用户名和密码或使用用户名和密码进行身份验证
     *
     * @param config Security配置
     * @return 身份验证管理器
     * @throws Exception 若为空值或获取失败
     */
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
        log.info("身份验证器初始化完成!");
        return config.getAuthenticationManager();
    }

}
